package com.zhny.aiot.bjxy.mico.pressure.plugin;

import com.zhny.aiot.bjxy.mico.pressure.plugin.model.MicroPressureStoreEntry;
import com.zhny.aiot.plugin.ICenterFacilities;
import com.zhny.aiot.plugin.IDeviceFactory;
import com.zhny.aiot.plugin.core.model.IRequestMessage;
import com.zhny.aiot.plugin.modbus.ModemEngine;
import com.zhny.aiot.plugin.modbus.device.ModemConfig;
import com.zhny.aiot.plugin.modbus.device.modbus.ModbusNumDevice;
import com.zhny.aiot.plugin.modbus.handler.model.ModemResponseMessage;
import com.zhny.aiot.plugin.model.DeviceEntity;
import com.zhny.aiot.plugin.model.TaskEntity;

public class MicroPressureDevice extends ModbusNumDevice<MicroPressureStoreEntry> {
    public MicroPressureDevice(ModemEngine engine, IDeviceFactory<ModemConfig> factory, ICenterFacilities centerFacilities, DeviceEntity entity) {
        super(engine, factory, centerFacilities, entity);
    }

    @Override
    public MicroPressureStoreEntry decode(ModemResponseMessage modemResponseMessage) {
        MicroPressureStoreEntry entry = new MicroPressureStoreEntry();
        int pressure = modemResponseMessage.getData().readUnsignedShort();
//        int depth = StringUtils.isNullOrEmpty(config.getDepth())?0:Integer.parseInt(config.getDepth());
        entry.setPressure (pressure/2000.0);
        return entry;
    }

    @Override
    public boolean checkError(ModemResponseMessage modemResponseMessage) {
        return modemResponseMessage.getData().readableBytes()== 2;
    }

    @Override
    protected IRequestMessage getCommand(TaskEntity taskEntity) {
        return getFactory().getRequestMessage(getConfig());
    }

    @Override
    protected void disposable() {

    }
}
